<template>
  <div class="app-container">
    <!-- 查询 -->
    <tl-list-search ref="search" :query-param="queryParam" @search="doQuery" :isDisplayDefaultSearch="false" :isDisplaySearch="false" v-if="isHeaderDisplay">
      <template slot="search">
        <!-- <el-form-item prop="code" v-if="editFlag">
          <label>项目</label>
          <el-input v-model="queryParam.qry_projectname" :disabled="true" class="tl-ref-selector" style="width:200px">
            <el-button slot="append" type="primary" icon="el-icon-search" @click="openPmProjectRefDialog"></el-button>
          </el-input>
        </el-form-item> -->
        <el-form-item label="项目名称">
          <el-input v-model="queryParam.qry_projectName" @change="changeprojectName"></el-input>
        </el-form-item>
        <el-form-item label="项目编号">
          <el-input v-model="queryParam.qry_projectCode" style="width:130px" @change="changeprojectCode"></el-input>
        </el-form-item>
        <el-form-item label="项目经理">
          <el-input v-model="queryParam.qry_projectManager" placeholder="姓名/工号" style="width:130px" @change="changeprojectManager"></el-input>
        </el-form-item>
        <el-form-item prop="qry_begindocdate">
          <label>单据日期</label>
          <el-date-picker v-model="queryParam.qry_begindocdate" type="date" style="width:150px" value-format="yyyy-MM-dd" placeholder="开始时间" @change="changebegindocdate" />至
          <el-date-picker v-model="queryParam.qry_enddocdate" type="date" style="width:150px" value-format="yyyy-MM-dd" placeholder="结束时间" @change="changeenddocdate" />
        </el-form-item>
        <el-form-item>
          <label>单据状态</label>
          <el-select v-model="queryParam.qry_state" clearable @change="changestate">
            <el-option v-for="item in codeModel.stateList" :key="item.code" :label="item.name" :value="item.code" clearable />
          </el-select>
        </el-form-item>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="doQuery">搜索</el-button>
        <el-button class="filter-item" type="primary" icon="el-icon-refresh-right" @click="resetQueryParam" v-if="editFlag" :disabled="isreset">重置</el-button>
      </template>
    </tl-list-search>
    <!-- 工具栏 -->
    <tl-list-toolbar ref="toolbar" v-if="editFlag" @add="openAddDialog('editForm')" @exportExcel="doExportExcel" :isDisplayExport="false" :isDisplayDeleteBatch="false" @deleteBatch="doDeleteBatch" @refresh="doRefresh" @columnCustomize="doColumnCustomize" @displaySearch="doDisplaySearch" :disableDeleteBatch="selectedRowKeys.length <= 0">
      <template slot="left-btn"> </template>
    </tl-list-toolbar>
    <div>
      <!-- 列表选择 -->
      <div class="table-alert-div" ref="selector">
        <el-alert type="success" :closable="false" class="tl-table-alert-info" effect="light">
          <template slot="title">
            已选择&nbsp;<a class="tl-table-alert-info-a1">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
            <a class="tl-table-alert-info-a2" @click="doClearSelected">全部清空</a>
          </template>
        </el-alert>
      </div>
      <!--表格渲染-->
      <el-table class="tl-table" ref="mainTable" :height="tableHeight" resizable-key="tableHeight" :resizable-refs="tableResizeRefs" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" row-key="id" border fit :row-class-name="doChangeRowClassName">
        <el-table-column type="index" label="序号" width="55px" align="center"></el-table-column>
        <el-table-column v-if="editFlag" prop="dispprojectid" label="项目" width="250px" sortable="custom" sort-by="projectid" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column v-if="editFlag" prop="code" label="项目编号" width="130px" sortable="custom" sort-by="code" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column v-if="editFlag" prop="projectmanagername" label="项目经理" width="110px" sortable="custom" sort-by="projectmanagername" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column v-if="editFlag" prop="projectmanagercode" label="项目经理工号" width="110px" sortable="custom" sort-by="projectmanagercode" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="closedate" label="闭环日期" width="170px" sortable="custom" sort-by="closedate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="summary" label="项目总结" width="250px" sortable="custom" sort-by="summary" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="latestphase" label="最新阶段" width="150px" sortable="custom" sort-by="latestphase" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="dispstate" label="状态" width="100px" sortable="custom" sort-by="state" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <!-- <el-table-column prop="orgname" label="申请公司" width="200px" sortable="custom" sort-by="orgname" align="left" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column prop="orgcode" label="申请公司编码" width="150px" sortable="custom" sort-by="orgcode" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column> -->
        <el-table-column prop="deptname" label="申请部门" width="200px" sortable="custom" sort-by="deptname" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="deptcode" label="申请部门编码" width="150px" sortable="custom" sort-by="deptcode" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="username" label="申请人姓名" width="150px" sortable="custom" sort-by="username" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="usercode" label="申请人工号" width="120px" sortable="custom" sort-by="usercode" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="docdate" label="单据日期" width="170px" sortable="custom" sort-by="docdate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="submitdate" label="提交时间" width="170px" sortable="custom" sort-by="submitdate" align="center" header-align="center" :formatter="dateTimeFormat" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="docno" label="单据编号" width="150px" sortable="custom" sort-by="docno" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="dispflowid" label="审批流程" width="200px" sortable="custom" sort-by="flowid" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="description" label="备注" min-width="250px" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <!-- <el-table-column label="操作" width="220px" v-slot="scope" fixed="right">
          <el-button type="text" icon="el-icon-edit" @click="openEditDialog('editForm', scope.row.id)">编辑</el-button>
          <el-button type="text" icon="el-icon-delete" :disabled="scope.row.state !== 0 && scope.row.state !== 2" @click="doDelete(scope.row.id)">删除</el-button>
          <el-dropdown trigger="hover">
            <el-button type="text" icon="el-icon-s-operation" style="margin-left:10px">更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu>
              <el-dropdown-item icon="el-icon-view" @click.native="openViewDialog('editForm', scope.row.id)">查看</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-table-column> -->
        <el-table-column label="操作" width="100px" v-slot="scope" fixed="right">
          <el-dropdown trigger="hover">
            <el-button type="text" icon="el-icon-s-operation" style="margin-left:10px">操作<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu>
              <el-dropdown-item icon="el-icon-view" @click.native="openViewDialog('editForm', scope.row.id)">查看</el-dropdown-item>
              <el-dropdown-item icon="el-icon-edit" @click.native="openEditDialog('editForm', scope.row.id)" :disabled="scope.row.state !== 0 && scope.row.state !== 2">编辑</el-dropdown-item>
              <el-dropdown-item icon="el-icon-delete" :disabled="(scope.row.state !== 0 && scope.row.state !== 2) || !editFlag" @click.native="doDelete(scope.row.id)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-table-column>
      </el-table>
      <!-- 列表分页 -->
      <el-row class="pagebar" ref="pager">
        <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
      </el-row>
      <tl-edit ref="editForm" @refresh="doRefresh" :projectId="projectId" :editFlag="editFlag"></tl-edit>
      <tl-pm-project-ref ref="pmProjectRef" @confirm="getPmProjectRefData" :isSingleSelect="true"></tl-pm-project-ref>
    </div>
  </div>
</template>

<script>
/** mixins */
import { mixin_list } from '@tapui/mixins/mixin_list'
import { checkPermission } from '@tapui/utils/permission'
/** 子组件 */
import tlEdit from './edit'
import { validateNull } from '@tapui/utils/validate'
import tlPmProjectRef from '@/views/ProjectManage/pmProject/refList'
/** 工具类 */
import { getAction, putAction, postAction, deleteAction } from '@tapui/utils/http'
export default {
  name: 'pmCloseApply',
  mixins: [mixin_list],
  components: { tlEdit, tlPmProjectRef },
  data() {
    return {
      description: '项目闭环申请-列表查询',
      urlPrefix: '/pm/closeApply/',
      url: {
        queryCodeList: '/pm/closeApply/dataCodeList'
      },
      // 排序参数
      sorterInfo: {
        sidx: 'pmcloseApply.createon',
        sord: 'desc'
      },
      queryParam: {
        qry_projectid: this.projectId
      },
      // 当前页面的编辑状态
      editFlag: true,
      reduceHeight: 325,
      isreset: false
    }
  },
  props: {
    projectId: {
      type: [Number, String],
      default: function() {
        return ''
      }
    },
    // 模块是否可以编辑
    moduleEditFlag: {
      type: Number,
      default: function() {
        return 1
      }
    },
    // 当前登录人角色 PM--项目经理 PP--项目参数人 OTHER--其他
    currentRole: {
      type: String,
      default: function() {
        return 'PM'
      }
    }
  },
  methods: {
    changeprojectName() {
      if (JSON.stringify(this.queryParam) !== '{}' && this.queryParam.qry_projectName !== '') {
        this.isreset = false
      } else {
        this.isreset = true
      }
    },
    changeprojectCode() {
      if (JSON.stringify(this.queryParam) !== '{}' && this.queryParam.qry_projectCode !== '') {
        this.isreset = false
      } else {
        this.isreset = true
      }
    },
    changeprojectManager() {
      if (JSON.stringify(this.queryParam) !== '{}' && this.queryParam.qry_projectManager !== '') {
        this.isreset = false
      } else {
        this.isreset = true
      }
    },
    changebegindocdate() {
      if (JSON.stringify(this.queryParam) !== '{}' && this.queryParam.qry_begindocdate !== '') {
        this.isreset = false
      } else {
        this.isreset = true
      }
    },
    changeenddocdate() {
      if (JSON.stringify(this.queryParam) !== '{}' && this.queryParam.qry_enddocdate !== '') {
        this.isreset = false
      } else {
        this.isreset = true
      }
    },
    changestate() {
      if (JSON.stringify(this.queryParam) !== '{}' && this.queryParam.qry_state !== '') {
        this.isreset = false
      } else {
        this.isreset = true
      }
    },
    afterLoadData() {
      this.isreset = false
      if (typeof this.queryParam.qry_projectName === 'undefined' && typeof this.queryParam.qry_projectCode === 'undefined' &&
        typeof this.queryParam.qry_projectManager === 'undefined' && typeof this.queryParam.qry_begindocdate === 'undefined' &&
        typeof this.queryParam.qry_enddocdate === 'undefined' && typeof this.queryParam.qry_state === 'undefined') {
        this.isreset = true
      }
    },
    /** 表格列排序 */
    doSortChange(columnInfo, prop, order) {
      if (columnInfo.column === null) {
        return
      }
      // 不排序
      if (validateNull(columnInfo.order)) {
        Object.assign(this.sorterInfo, this.sorterDefaultInfo)
        this.loadData({})
        return
      }
      // 判断是否处理垃圾数据
      if (columnInfo.column.sortBy.indexOf('.') !== -1) {
        columnInfo.column.sortBy = columnInfo.column.sortBy.substring(columnInfo.column.sortBy.lastIndexOf('.') + 1)
      }
      if (columnInfo.prop.indexOf('.') !== -1) {
        columnInfo.prop = columnInfo.prop.substring(columnInfo.prop.lastIndexOf('.') + 1)
      }
      if (columnInfo.column.sortBy === 'code' || columnInfo.column.sortBy === 'projectmanagercode' || columnInfo.column.sortBy === 'projectmanagername') {
        // 升序或者降序排序
        if (columnInfo.column.sortBy) {
          columnInfo.column.sortBy = 'PmProject.' + columnInfo.column.sortBy
          this.sorterInfo.sidx = columnInfo.column.sortBy
        } else {
          columnInfo.prop = 'PmProject.' + columnInfo.prop
          this.sorterInfo.sidx = columnInfo.prop
        }
      } else {
        // 升序或者降序排序
        if (columnInfo.column.sortBy) {
          columnInfo.column.sortBy = 'pmcloseApply.' + columnInfo.column.sortBy
          this.sorterInfo.sidx = columnInfo.column.sortBy
        } else {
          columnInfo.prop = 'pmcloseApply.' + columnInfo.prop
          this.sorterInfo.sidx = columnInfo.prop
        }
      }
      this.sorterInfo.sord = columnInfo.order.replace('ending', '')
      this.loadData({})
    },
    // 禁用表格前面的选择
    doDisableCheckBox(row, index) {
      if (row.state !== 0 && row.state !== 2) {
        return false
      } else {
        return true
      }
    },
    /** 打开项目参照 */
    openPmProjectRefDialog() {
      this.$refs.pmProjectRef.visible = true
    },
    /** 获取项目参数数据 */
    getPmProjectRefData(selectionRow, selectKey, selectionValue) {
      const { id, name } = selectionRow
      this.$set(this.queryParam, 'qry_projectid', id)
      this.$set(this.queryParam, 'qry_projectname', name)
    },
    resetQueryParam() {
      this.queryParam = {}
      this.changeprojectName()
      this.changeprojectCode()
      this.changeprojectManager()
      this.changebegindocdate()
      this.changeenddocdate()
      this.changestate()
    },
    // /** 表格双击事件 */
    // doRowDbClick(row, column, event) {
    //   if (this.rowDbClickActionType === 'edit' && this.editFlag) {
    //     this.openEditDialog('editForm', row.id)
    //   } else {
    //     this.openViewDialog('editForm', row.id)
    //   }
    // },
    /** 表格双击事件 */
    doRowDbClick(row, column, event) {
      // 判断双击打开是查看还是编辑
      if (row.state === 0 || row.state === 2) {
        this.rowDbClickActionType = 'edit'
      } else {
        this.rowDbClickActionType = 'view'
      }
      if (this.rowDbClickActionType === 'edit') {
        // 判断是否有权限
        if (this.editPermission.length > 0) {
          if (checkPermission(this.editPermission)) {
            this.openEditDialog('editForm', row[this.$refs.mainTable.rowKey])
          } else {
            this.openViewDialog('editForm', row[this.$refs.mainTable.rowKey])
          }
        } else {
          this.openEditDialog('editForm', row[this.$refs.mainTable.rowKey])
        }
      } else if (this.rowDbClickActionType === 'view') {
        this.openViewDialog('editForm', row[this.$refs.mainTable.rowKey])
      }
    },
    initParam() {
      this.editFlag = this.moduleEditFlag === 1 && this.currentRole === 'PM'
      this.reduceHeight = this.editFlag ? 134 : 325
      this.resizeHandler()
    },
    searchList(projectId) {
      if (!this.editFlag) {
        this.queryParam.qry_projectid = projectId
        this.loadData({})
      }
    }
  },
  watch: {
    projectId(newValue, oldValue) {
      this.searchList(newValue)
    }
  },
  mounted() {
    this.initParam()
  }
}
</script>

<style scoped></style>

